var DAO=require('./DAO')
//针对用户数据表操作的模块对象
var usersDAO={
    //获取某个用户所有信息
    getUserById:function(user,callback){
        DAO('select * from users where phone = ?',user.phone,function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },

    //注册
    register:function(user,callback){
        DAO('insert into users(phone,userPassword) values(?,?)',[user.phone,user.userPassword],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    //登录（根据手机号）
    getUserPhone:function(phone,callback){
        DAO('select * from users where phone = ?',[phone],function(err,results){
            // console.log(results)
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    //修改密码
    getUserPassword:function(user,callback){
        DAO('select userPassword from users where userId=(select userId from users where phone = ?)',user.phone,function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    changePassword: function (user, callback) {
        console.log(user.loginuser.phone,user.userPassword)
        DAO('update users set userPassword = ? where phone = ?',[user.userPassword,user.loginuser.phone],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },

    //修改用户名
    changeUsername:function(user,callback){
        DAO('update users set userName=? where phone=?',[user.userName,user.loginuser.phone],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    //上传头像
    upload:function(user,callback){
        DAO('update users set headPic = ? where phone = ?',[user.headPic,user.loginuser.phone],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    //增加积分数
    addintegral:function(user,callback){
        DAO('update users set integral=integral+2 where phone = ?',user.phone,function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },

    //兑换积分
    //查看兑换的积分数（步骤一）
    lookgenitegral:function(shopId,callback){
        DAO('select getintegral from shop where shopId=?',[shopId],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    //查看用户积分数
    userintegral:function(user,callback){
        DAO('select integral from users where phone=?',user.phone,function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    //用户积分数减少
    subuserintegral:function(small,big,user,callback){
        console.log("小"+small)
        console.log("大"+big)
        DAO('update users set integral=?-? where phone=?',[big,small,user.phone],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    //增加兑换记录
    addexchange:function(user,shopId,callback){
        DAO('insert into exchange(exchangeTime,touserId,toshopId) values(now(),(select userId from users where phone = ?),?)',[user.phone,shopId],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    }
    
}
module.exports=usersDAO